package com.wss.lsl.lucene.demo.shipin.analyzer;

import java.io.Reader;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.LetterTokenizer;
import org.apache.lucene.analysis.LowerCaseFilter;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.util.Version;

// 自定义分词器
public class MyStopAnalyzer extends Analyzer {

	@SuppressWarnings("rawtypes")
	private Set stops;

	@SuppressWarnings("unchecked")
	public MyStopAnalyzer(String[] sws) {
		// 自动将字符串数组转化为set
		stops = StopFilter.makeStopSet(Version.LUCENE_35, sws, true);
		// 英文默认停用词
		stops.addAll(StopAnalyzer.ENGLISH_STOP_WORDS_SET);
	}

	public MyStopAnalyzer() {
		stops = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
	}

	@Override
	public TokenStream tokenStream(String fieldName, Reader reader) {
		return new StopFilter(Version.LUCENE_35, new LowerCaseFilter(
				Version.LUCENE_35, new LetterTokenizer(Version.LUCENE_35,
						reader)), stops);
	}

}
